<?php
/* 地址修改 */
namespace Api\Controller;
use Think\Controller;
use Think\Log;
class PAddressController extends Controller {
    private $user = array();

    public function __construct()
	{
		//解决跨域
		echoHeader();
		$phone = I('post.phone', '', 'trim');
		// $phone = '13660392546';
        $this->user = session('p_user');
		if(!$this->user or $this->user['phone'] != $phone){
			$this->ajaxReturn(array('err_code'=>10000, 'err_msg'=>'请先登录'));
		}
        $this->user = M("PUser")->where(array("phone"=>$phone, "is_deleted"=>0))->find();
		if (!$this->user) {
			$this->ajaxReturn(array('err_code'=>10000, 'err_msg'=>'请先登录'));
		}
        $this->user['score']=user_score_sum($this->user['id']);
	}

	public function index()
	{
		$map['is_deleted']=0;
        $map['uid']=$this->user['id'];

		$address = M("PAddress")->where($map)->find();
		$this->ajaxReturn(array('err_code'=>200, 'err_msg'=>$address));
	}


	//报名
	public function save(){

        $map['is_deleted']=0;
        $map['uid']=$this->user['id'];
        $address = M("PAddress")->where($map)->find();
        $is_exists=true;
        if (!$address) {
            $is_exists=false;
            $address=array();
            $address['uid']=$map['uid'];
            $address['create_time']=time();
        }

        $address['province'] = I('post.province', 0, 'trim');
        $address['city'] = I('post.city', 0, 'trim');
        $address['area'] = I('post.area', 0, 'trim');
        $address['address'] = I('post.address', 0, 'trim');
        $address['delivery_time'] = I('post.delivery_time', 0, 'trim');
        $address['update_time']=time();

        if($is_exists){
            M("PAddress")->where($map)->save($address);
        }
        else{
            M("PAddress")->add($address);
        }

        return $this->ajaxReturn(array('err_code'=>200, 'err_msg'=>'更新完成'));

    }

	
}